package top.scsoul.boot.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录权限验证拦截器
 */
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("user");
        if (ObjectUtils.isEmpty(user)) {
            request.setAttribute("err","你还没有登录，请先登录！");
            request.getRequestDispatcher(request.getContextPath()+"/login").forward(request, response);
            return false;
        }
        return true;
    }
}
